//
// Created by win on 2017/2/20.
//


#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frbyte_buffer.h"
#include "../../../src/module/thrift/config.h"
#include "../../../src/module/thrift/tcompact/tcompact.h"
#include "../../../src/module/thrift/thrift_core.h"



#include "apmtest_cunit.h"
#include "apmtest_comm.h"
static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

extern int __read_varint32(Frbyte_Packet *p_packet, int *p_i_ret);


static void case1(void)
{
    Frbyte_Packet *p_packet = NULL;
    int i_ret = 0;

    i_ret = __read_varint32(p_packet, NULL);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    return;
}



static void case2(void)
{
    Frbyte_Packet *p_packet = bytebuff_creat();
    int i_ret = 0;
    int i_out = 0;

    bytebuff_add(p_packet, (char *)"\x0a", 1);
    i_ret = __read_varint32(p_packet, &i_out);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(i_out, 10);
    bytebuff_free(&p_packet);

    return;
}


static void case3(void)
{
    Frbyte_Packet *p_packet = bytebuff_creat();
    int i_ret = 0;
    int i_out = 0;

    bytebuff_add(p_packet, (char *)"\x82\xf1\x00", 3);
    i_ret = __read_varint32(p_packet, &i_out);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(i_out, 14466);
    bytebuff_free(&p_packet);

    return;
}

int add_suite___read_varint32(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __read_varint32()", __suite_init, __suite_cleanup)) == NULL)
    return FR_FAIL;

    CU_add_test(pSuite, "case1", case1);
    CU_add_test(pSuite, "case2", case2);
    CU_add_test(pSuite, "case3", case3);


    return FR_SUCC;

}